using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DependencyLib;
using CrossFlow.GraphLib;
using System.Reflection;
using System.IO;

namespace ConsoleWindow
{
	public partial class CLRDependsForm : Form
	{
		DirectedGraph graph;
		GraphDelta delta;
		DigraphAsVisioDrawingWriter visio;

//TODO: Source ListBox in Add/Remove arrows should allow multiple selection
//TODO: Filter out selected types

		public CLRDependsForm()
		{
			InitializeComponent();
			graph = new DirectedGraph(string.Empty);
			visio = new DigraphAsVisioDrawingWriter();
		}

		void log(string format,params object[] args)
		{
			LogListBox.Items.Add(string.Format(format,args));
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (folderBrowserDialog1.ShowDialog() != DialogResult.OK) return;
			this.RootFolderTextBox.Text = folderBrowserDialog1.SelectedPath;
		}

		private void LocateFileTargets(string folder, string filter,bool tree)
		{
			AssemblyFolderIterator files = new AssemblyFolderIterator(folder,filter,null,null,tree);
			foreach (Assembly assembly in files)
				SourceAssembliesListBox.Items.Add(assembly);
//      foreach (string name in files.NotFoundAssemblies) LogListBox.Items.Add(name);
		}

		private void LoadNamesButton_Click(object sender, EventArgs e)
		{
			LocateFileTargets(this.RootFolderTextBox.Text, this.FilterTextBox.Text,TreeCheckBox.Checked);
		}

		private void button3_Click(object sender, EventArgs e)
		{
			if(TargetAssembliesListBox.SelectedItem != null)
				TargetAssembliesListBox.Items.Remove(TargetAssembliesListBox.SelectedItem);
		}

		void GetDigraph()
		{
/*
      IDependencyProvider depends = new CLRDependencyProvider(new AssemblyIterator(TargetAssembliesListBox.Items, null), null);
			graph = depends.GetGraph();
			delta=depends.AppendToGraph(graph);
			graph.Name = this.NameTextBox.Text;
			log("Graph name:{0}", graph.Name);
			log("VertexCount:{0}", graph.VertexCount);
			log("EdgeCount:{0}", graph.EdgeCount);
			log("ContainerGraph null?:{0}", graph.ContainerGraph == null);
			log("InitialVertex:{0}", graph.InitialVertex.Name);
*/
/*			log("Vertexes:");
			foreach (Vertex v in graph.Vertexes)
				log("Vertex:{0}", v.Name);
			foreach (Edge e in graph.Edges)
				log("({0}->{1})", e.Source.Name, e.Target.Name);*/
		}

		private void GetDependenciesButton_Click(object sender, EventArgs args)
		{
			GetDigraph();
		}

		private void QuitButton_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void PersistToVisio()
		{
			if(this.OverrideRadioButton.Checked)
				using (StreamWriter writer = File.CreateText(this.NameTextBox.Text))
				{
					visio.Write(writer,graph);
				}
			else if (this.AppendRadioButton.Checked)
			{
				string current_xml=string.Empty;
				using(StreamReader reader=File.OpenText(this.NameTextBox.Text))
				{
					current_xml=reader.ReadToEnd();
				}
				using (StreamWriter writer = File.CreateText(this.NameTextBox.Text))
				{
					visio.Append(current_xml,delta,writer);
				}
			}
		}

		private void VisioSaveButton_Click(object sender, EventArgs e)
		{
			PersistToVisio();
		}

		private void CompileButton_Click(object sender, EventArgs e)
		{
			GetDigraph();
			PersistToVisio();
		}

		private void SelectFileButton_Click(object sender, EventArgs e)
		{
			if (folderBrowserDialog1.ShowDialog() != DialogResult.OK) return;
			this.NameTextBox.Text = folderBrowserDialog1.SelectedPath + "\\" + graph.Name + ".vdx";
		}

		private void ToRightButton_Click(object sender, EventArgs e)
		{
			this.TargetAssembliesListBox.Items.Add(this.SourceAssembliesListBox.SelectedItem);
			this.SourceAssembliesListBox.Items.Remove(this.SourceAssembliesListBox.SelectedItem);
    }
	}
}